package com.hcxdi.app.droidbase.presenter;


import com.hcxdi.api.bean.auth.Auth;
import com.hcxdi.api.bean.auth.AuthReq;
import com.hcxdi.api.bean.base.Rsp;
import com.hcxdi.app.droidbase.contract.AuthContract;
import com.hcxdi.app.droidbase.model.AuthModel;
import com.hcxdi.base.presenter.RxPresenter;
import com.hcxdi.base.presenter.RxSubscriber;

/**
 * TODO
 *
 * @author 成都青桔科技有限公司
 * @version 1.0
 * @changelog 16-11-9 初始化
 */
public class AuthPresenter extends BasePresenter<AuthContract.View> implements AuthContract.Presenter {

    AuthContract.Model mModel;

    public AuthPresenter() {
        mModel=new AuthModel();
    }
    @Override
    public void loadAuth(AuthReq req) {
        mModel.loadAuth(req).subscribe(new RxSubscriber<Rsp<Auth>,BasePresenter<AuthContract.View>>(this) {
            @Override
            public void onSuccess(Rsp<Auth> authRsp) {
                getView().onAuthed(authRsp.getData());
            }
        });
             /*   mModel.loadAuth(req)
                .subscribe(new Subscriber<Rsp<Auth>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        addSubscription(this);
                        getView().onShow();
                    }

                    @Override
                    public void onCompleted() {
                        removeSubscription(this);
                        getView().onHide();
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().onHide();
                        getView().onError(e.getMessage());
                    }

                    @Override
                    public void onNext(Rsp<Auth> authRsp) {
                        getView().onAuthed(authRsp.getData());
                    }
                });*/
    }

    /*@Override
    public void loadAuth(AuthReq req) {
        getView().onShow();
        AuthService authService= HcxdiAPI.getInstance(mOkHttpBuilder.build(),baseUrl)
                .createService(AuthService.class);
        final UserService userService= HcxdiAPI.getInstance(mOkHttpBuilder.build(),baseUrl)
                .createService(UserService.class);
        //mModel.loadAuth()
        Observable authObserable=   authService.auth(req)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<Rsp<Auth>, Boolean>() {
                    @Override
                    public Boolean call(Rsp<Auth> authRsp) {
                        return authRsp==null?false:authRsp.getCode()==0;
                    }
                })
                .flatMap(new Func1<Rsp<Auth>, Observable<?>>() {
                    @Override
                    public Observable<?> call(Rsp<Auth> authRsp) {
                        return userService.userInfo(authRsp.getData().getToken());
                    }
                });
                *//*.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
                    @Override
                    public Observable<?> call(Observable<? extends Throwable> observable) {
                        return null;
                    }
                })*//*
                //.retry(1000*1)//TimeUnit.SECONDS
                *//*.concatMap(new Func1<Rsp<Auth>, Observable<?>>() {
                    @Override
                    public Observable<?> call(Rsp<Auth> authRsp) {
                        return null;
                    }
                })*//*
                //.compose(null)
        authObserable.subscribe(new Observer<Rsp<Auth>>() {
                    @Override
                    public void onCompleted() {
                        getView().onHide();
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().onHide();
                        getView().onError(e.getMessage());
                    }

                    @Override
                    public void onNext(Rsp<Auth> authRsp) {
                        getView().onAuthed(authRsp.getData());
                    }
                });
    }*/



    private void load(){
       /* Observable.just(1)
                .observeOn(Schedulers.io())
                .map(new Func1<Integer, List<RankItem>>() {
                    @Override
                    public List<RankItem> call(Integer integer) {
                        return model.moreRankItems();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<RankItem>>() {
                    @Override
                    public void call(List<RankItem> list) {
                        if (list !=null &&list.size()!=0)
                            view.onMoreRanks(list);
                        else view.onRankFailure("load null");
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        view.onRankFailure(throwable.getMessage());
                    }
                });*/
    }


}
